<?php    
    define('PIXEL_MODE', 'development');
    require "../pixel/config/environment.php";
    #$GLOBALS['PIXEL']['DEBUG_LEVEL'] = 1;
    require "../pixel/scripts/installer.php";			
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Language" content="en-us" />
		
		<title>Pixel App Installer</title>				
		<?php echo javascript_defaults()?>
        <?php echo javascript_form_defaults()?>
        <link rel="stylesheet" href="<?php echo get_installed_path() ?>/css/forms.css" type="text/css" media="screen" title="no title" charset="utf-8" />
		<style type="text/css" media="screen">
		/* <![CDATA[ */
		  body {
		      background: #e3e3e3;
		      font-family:arial,san-serif;
		      font-size:medium;
		      margin:0;
		      padding:0;
		  }
		  #wrap {
		      border:3px solid #999;
		      background: #fff;
		      padding:15px;
		      width:500px;
		      margin:0 auto;
		      border-top:none;
		  }
		  h1 {
		      color:blue;
		      text-align:center;
		  }
		  hr {
		      border:2px dashed #999;
		  }
		  li {
		      margin:10px auto;
		  }
		  td {
		      vertical-align:top;
		  }
		  #step_message {
		      width:250px;
		      margin:0 auto;
		  }
		/* ]]> */
		</style>					
	</head>
	
	<body id="">
        <div id='wrap'>
	    <h1>Installing Pixel-App</h1>
	    
	    <div id='step_message'>
	    <?php 
	    $success_text = false;
	    if ( $_POST['action'] == 'next_step' )
    	{
    	    $errors = array();
    	    // validate input
    	    do {
    	        if ( empty($_POST['first_name']) )
    	        {
    	            $errors['first_name'] = 'First Name can not be blank.';
    	        }
    	        if ( empty($_POST['last_name']) )
    	        {
    	            $errors['last_name'] = 'Last Name can not be blank.';
    	        }
    	        if ( !is_valid_email($_POST['email']) )
    	        {
    	            $errors['email'] = 'Email address is invalid.';
    	        }
    	        if ( strlen($_POST['user_password'])<6 )
    	        {
    	            $errors['password'] = 'Your password must be at least 6 characters long.';
    	        }
    	        if( sizeof($errors) > 0 ) break;
    	        $success_text = install_pixel_app();
    	    } while(0);
    	    
    	    if ( !empty($errors) )
    	    {
    	        $header_tag = "h2";
            	$id			= 'error';
            	$class		= 'div_form_error_list';    	    	

        		$header = content_tag($header_tag,'Error!');
        		$body   = content_tag('p','Your form contained the following errors:');
        		foreach( $errors as $key => $value )
        		{
        		    $key     = ucwords(str_replace("_"," ",$key));
        			$f_errors .= content_tag('li',"$key: $value");
        		}
        		$error_list = content_tag('ul',$f_errors);
        		echo content_tag('div',$header.$body.$error_list,array('class'=>$class,'id'=>$id));
    	    }
	    }
	    // did it install?
	    if ($success_text) 
	    {
	        echo $success_text;
	    } else { 
	        if (!pre_flight_check()) {
        ?>
        <h2>Install Errors</h2>
        <p>Before you begin, you will have to take care of the following things:</p>
        <div class='div_form_error_list'>
        <ul><?php foreach($pre_install_errors as $e) { echo "<li>$e</li>";} ?></ul>
        </div>
        <p><a href="#" onclick="javascript:window.location.reload(false);return false">Refresh the page</a> when you have made the changes.</p>
        <?php	            
	        } else {
	    ?>	     
        <h2>Step One</h2>
	    <p>Please fill out the following fields:</p>
	    <?php echo  form_tag(get_installed_path().'/install.php',array('validation'=>true)) ?>
	                                
	    <?php echo  hidden_field_tag('action','next_step') ?>
	    <ol>	       
	       <li><b>First Name:</b><br>
	           <?php echo  text_field_tag('first_name', $_POST['first_name'], array('alt'=>'blank')) ?></li>
	       <li><b>Last Name:</b><br>
	           <?php echo  text_field_tag('last_name', $_POST['last_name'], array('alt'=>'blank')) ?></li>
	       <li><b>Email address:</b><br>
	           <?php echo  text_field_tag('email', $_POST['email'], array('alt'=>'email')) ?></li>
	       <li><b>Desired password:</b> (must be at least 6 characters long)<br>
	           <?php echo text_field_tag('user_password', $_POST['user_password'], array('alt'=>'length|6'))?>
	    </ol>	
	    <p style='padding-left:45px;'><?php echo  submit_tag('Next') ?></p>    
	    <?php echo  end_form_tag() ?>
	        <?php } ?>
	    <?php } ?>
        </div>
	    </div>    
	</body>
</html>

